import type { Effect, Reducer } from 'umi';

import { queryCurrent,queryPrice } from '@/services/user';
import { setAuthority } from '@/utils/authority';

export type CurrentUser = {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  tags?: {
    key: string;
    label: string;
  }[];
  userId?: string;
  unreadCount?: number;
};

export type UserModelState = {
  currentUser?: CurrentUser;
};

export type UserModelType = {
  namespace: 'user';
  state: UserModelState;
  effects: {
    // fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<UserModelState>;
    savePrice:Reducer<any>;
    // changeNotifyCount: Reducer<UserModelState>;
  };
};

const UserModel: UserModelType = {
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
    // *fetch(_, { call, put }) {
    //   const response = yield call(queryUsers);
    //   yield put({
    //     type: 'save',
    //     payload: response,
    //   });
    // },

    *fetchCurrent(_, { call, put }) {
      //处理所有价格等级
      const prices = yield call(queryPrice);
      const pricesData = prices.data 
      yield put({
        type: 'savePrice',
        payload: pricesData,
      });
      const response = yield call(queryCurrent);
      const data = response.data
      yield put({
        type: 'saveCurrentUser',
        payload: data,
      });
    },
  },

  reducers: {
    savePrice(state,action){
      sessionStorage.setItem('prices',JSON.stringify(action.payload))
    },
    saveCurrentUser(state, action) {
      sessionStorage.setItem('factories',JSON.stringify(action.payload.factories.map((item:any)=>{
        return {value:Number(item.manufacturer),label:item.shortName,flag:item.deleteFlag}
      })))
      sessionStorage.setItem('isOrder',action.payload.isOrder)
      sessionStorage.setItem('isShowAttach',action.payload.isShowAttach)
      if(action.payload.access==='jxs-1'||action.payload.access==='jxs2-1'){
        if(action.payload.level==='一级'){
          setAuthority(action.payload.access);
        }else{
          setAuthority('dealer2');
        }
      }else{
        if(action.payload.level==='一级'){
          setAuthority(action.payload.access);
        }else{
          // setAuthority(action.payload.access.replace(/jxs/, jxs2));
          sessionStorage.setItem('sale_type',action.payload.SalesType);
          if(action.payload.SalesType){
            if(action.payload.SalesType==='1'){
              setAuthority('sale0');
            }else{
              setAuthority(action.payload.access);
            }
          }else{
            setAuthority(action.payload.access);
          }
        }
      }
      sessionStorage.setItem('loginName',action.payload.name)
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    // changeNotifyCount(
    //   state = {
    //     currentUser: {},
    //   },
    //   action,
    // ) {
    //   return {
    //     ...state,
    //     currentUser: {
    //       ...state.currentUser,
    //       notifyCount: action.payload.totalCount,
    //       unreadCount: action.payload.unreadCount,
    //     },
    //   };
    // },
  },
};

export default UserModel;